public class Student {
    private int age;
    private String name;
    private int[] score=new int[3];
    private int scorelimit=100;
    public Student(){           //默认
        this.age=18;
        this.name="未命名";
        this.score= new int[]{0, 0, 0};
    }
    public Student(String name,int age, int[] score){           //有输入的话就调用这个
        try {
            this.age=age;
            this.name=name;
            setScore(score);
        } catch (ArrayIndexOutOfBoundsException e1) {       //防止成绩胡乱输入
            if (score.length>3) {
                System.out.println("成绩输入格式不对，已默认设为前三个输入");
                this.score = new int[]{score[0], score[1], score[2]};
            }else {
                System.out.println("成绩输入格式不对，缺失部分默认为零");
                for(int i=0;i<score.length;i++){
                    this.score[i]=score[i];
                }
            }
        }
    }
    public int getAge(){            //获得年龄
        return this.age;
    }
    public String getName(){            //获得名字
        return this.name;
    }
    public int[] getScore(){            //获得成绩
        return this.score;
    }
    public void setAge(int age){        //设置年龄并防止不对的输入
        if(age>0){
            this.age=age;
        }else {
            System.out.println("输入年龄不合理，已取消本次修改");
        }
    }
    public void setName(String name){       //设置名字
        if(name != null&& !name.isEmpty()) {
            this.name = name;
        }else {
            System.out.println("识别出空白输入，已取消本次修改");
        }
    }
    public void setScore(int[] score){      //设置成绩
        for (int i = 0; i < score.length; i++) {
            try {
                if(score[i]>=0&&score[i]<=scorelimit) {
                    this.score[i] = score[i];
                }else {
                    System.out.println("位于第"+(i+1)+"位置的成绩数值不合理，已默认设置为0");
                }
            } catch (IndexOutOfBoundsException e) {
                System.out.println("输入格式可能出现错误，修改已取消");
            }
        }

    }
    public double calculateAverage(){       //计算平均成绩
        double result=0;
        for (int j : score) {
            result = result + j;
        }
        return result/score.length;
    }
}
